package com.yck12.v1;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class Main3 {

    public static void main(String[] args) throws IOException {
        //从硬盘上的csv文件中读取中文姓名列表
//        String temp="李玉秀, 梁雁, 刘静, 刘铭, 刘煜, 马建华, 马青云, 米热古丽·买买提, 牛晓红, 秦贵军, 秦映芬, 全会标, 冉兴无, 单忠艳, 施秉银, 时立新, 苏本利, 苏恒, 苏青, 田利民, 卫海燕, 吴迪, 向光大, 肖海鹏, 武晓泓, 徐积兄, 徐静, 闫朝丽, 严晋华, 严孙杰, 袁慧娟, 张梅, 张一宁, 郑雪瑛, 朱虹, 胡婧宜, 吴蔚, 谢雨婷, 肖扬, 黄干, 邓超, 曹楚晴, 罗说明, 范黎, 郭柯宇, 蔡晓凌, 程昕然, 代喆, 房辉, 高莹, 黄晓燕, 李海燕, 李秀珍, 励丽, 梁丽俊, 刘德云, 刘毓, 罗建华, 潘洁敏, 权金星, 孙妍, 王桂侠, 王颜刚, 王育璠, 任韵清, 王锐, 白冠男, 曹冰燕, 古建平, 顾楠, 郭璐璐, 黄轲, 姜天, 李燕虹, 刘春妍, 汪治华, 王双宇, 吴薇, 吴利平, 邢秋玲, 徐晶晶, 袁婷, 袁红娣, 郑燕, 彭晓霞, 钱素云, 肖新华, 常波, 陆国平, 陈瑞敏, 范歆, 杜红伟, 顾威, 冯梅, 陈晓波, 刘珺, 刘雅莉, 王秀敏, 郑荣秀, 姚辉, 郭红, 王春林, 辛颖, 李沛衡, 李亚铮, 舒伟祁, 解凯铭, 程明, 窦梓钰, 何子君, 靳景璐, 刘美娟, 孟曦, 任潇亚, 宋艳宁, 闫牧乔, 袁峥, 张会丰, 宋文惠, 刘霞, 刘戈力, 熊晖, 杨艳玲, 陈少科, 崔岚巍, 成胜权, 王安茹, 张惠文, 董国庆, 李桂梅, 孟英韬, 梁黎, 王伟, 张星星, 程若倩, 倪锦文, 裴舟, 孙成君, 奚立, 章淼滢, 赵诸慧, 彭永德, 石勇铨, 冯波, 徐先明, 潘曙明, 胡予, 滕卫平, 包玉倩, 陈耀龙, 何玉玲, 黄金, 李红, 孙子林, 童南伟, 汪志红, 王从义, 徐爱民, 薛耀明, 张驰, 张俊清, 郑宏庭, 超晨, 陈小燕, 方晨, 郭佳, 何斌斌, 何凌, 侯粲, 黄凡素, 贾晓凡, 金萍, 兰纯娜, 李娟, 刘芳, 陆婧, 时夏捷, 孙磊, 孙曙光, 汤晓涵, 王臻, 武超, 向宇飞, 徐蓉, 颜湘, 杨琳, 张梅, 张翼, 支涤静, 朱熊兆, 关海霞, 王文尧, 郑一天, 刘东方, 刘晨, 李永乐, 朱丽华, 闫承慧, 吕朝晖, 张海清, 陈刚, 折剑青, 杨晓蕾, 杨艳敏, 郑昭芬, 郭远林, 唐熠达, 徐标, 陶贵周, 校蕾, 崔丽艳, 程歆琦, 黎励文, 陈开宁, 高政南, 李强, 刘建英, 吕肖锋, 陶静, 郗光霞, 许樟荣, 尹士男, 赵芳, 赵维刚, 宋怀东, 李思进, 田文, 刘金钢, 刘永锋, 李亚明, 冯晓云, 刘超, 李乃适, 李静, 张浩, 费阳, 高再荣, 王广, 汤旭磊, 杨刚毅, 陆志强, 陈兵, 陈宏, 周迎生, 侯建明, 侯新国, 徐玉善, 夏维波, 管庆波, 祝之明, 徐向进, 杨静, 宋光耀, 韩学尧, 窦京涛, 阚全娥, 鲁瑾, 王丽宏, 王清, 李军, 王敏哲, 谢晓敏, 张惠莉, 杨丽辉, 向菲, 徐勇, 梁瑜祯, 朱筠, 李焱, 高凌, 周新丽, 赖晓阳, 李学军, 叶山东, 吴天凤, 马向华, 张克勤, 陆颖理, 洪洁, 段滨红, 孙亚东, 李冬梅, 李兴, 朱铁虹, 袁戈恒, 于淼, 邢小燕, 王海宁, 李全民, 鲍鹏丽, 布力布丽.巴哈提, 陈秋莉, 陈婷, 陈曦, 陈永兴, 刁甜甜, 樊蕊, 何芳, 李鹏, 刘颖, 卢洪华, 吕拥芬, 马艳艳, 毛晓健, 孟哲, 潘丽, 皮亚雷, 任翼, 宋萃, 孙小妹, 孙之星, 唐芳, 田凤艳, 王倩, 肖园, 许珊珊, 杨敏, 张东光, 张尧, 张莹, 朱建芳, 王金玲, 张偲, 郭盛, 黄永兰, 李利, 李堂, 梁立阳, 刘炎洁, 陆炜, 马明圣, 米热古丽·热西提, 谢理玲, 邢杰, 俞丹, 赵云静, 袁国跃, 薛云, 曲伸, 蔡寒青, 刘喆隆, 鲁一兵, 安振梅, 陈立曙, 刘婕, 马瑜瑾, 闫德文, 叶军, 欧阳文, 张秀薇";
        String temp = "雷涛,胡吉,成志峰,成金罗,崔世维,蒋晓红,王志宏,董其娟,耿厚法,李伟,陆强,孙丽,皮银珍,莫朝晖,张亚萍,王彦军,李亚,杨淑芳,胡文,陆卫平,高家林,许岚,王昆,边芳,程霖,邓向群,杜玮,顾明君,井庆平,刘师伟,刘小伟,李红梅,毛莉,乔虹,尚军洁,沈洁,史育红,孙文利,田军航,王光亚,王连伟,王雪琴,王中京,奚悦,徐旭,杨架林,叶凯云,张改秀,张敏,张新,周建博,朱蕾";

        //使用逗号分割字符串，得到中文姓名列表
        String[] chineseNames = temp.split(",\\s*");
        //建立多人的中文姓名的列表，每个人占用一行，有四列，支持四种拼音名称，例如 郭立新,转换为：guo lixin, guo lx, guo lixin, guo lx
//        List<String> chineseNames = Arrays.asList(
//                "周志光", "朱敏", "翁建平", "纪立农", "郭立新", "杨涛", "傅君芬", "罗小平", "朱大龙", "赵家军", "母义明", "王天佑", "王卫庆", "洪天配", "宁光", "贾伟平", "肖建中", "严励", "严德文", "毕艳", "杨金奎", "杨昱", "余学锋", "曾天舒", "张弛", "张惠杰", "张丽辉", "张秋", "郑超", "周家强", "周健", "蔡梦茵", "陈莉", "陈黎明", "陈临琪", "董关平", "杜建玲", "龚春秀", "纪秋和", "姜宏伟", "姜生", "匡洪宇", "匡健", "雷闽湘", "李玲", "李频", "李启富", "李霞", "李小英", "李延兵", "李益明", "苏哲", "罗飞宏"
//        );

        List<String> names = new ArrayList<>();
        for (String chineseName : chineseNames) {
            // 使用pinyin4j将中文姓名转换为拼音
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(chineseName.charAt(0));
            String firstName = pinyinArray[0].replaceAll("\\d", "");
            String lastName = chineseName.substring(1);
            String[] lastNamePinyinArray = PinyinHelper.toHanyuPinyinStringArray(lastName.charAt(0));
            String[] lastName2PinyinArray =  null;
            String lastName2Pinyin = "";
            String lastNamePinyin = lastNamePinyinArray[0].replaceAll("\\d", "");
            //如果名字的汉字有两个，则取第二个汉字的拼音
            if (lastName.length() > 1) {
                lastName2PinyinArray = PinyinHelper.toHanyuPinyinStringArray(lastName.charAt(1));
                lastName2Pinyin = lastName2PinyinArray[0].replaceAll("\\d", "");
            }
            String format1 = "";
            String format2 = "";
            String format3 = "";
            String format4 = "";

            // 生成四种拼音格式
            if (lastName.length() > 1) {
                format1 = firstName + " " + lastNamePinyin + lastName2Pinyin;
                format2 = lastNamePinyin + lastName2Pinyin + " " + firstName;
                format3 = firstName + " " + lastNamePinyin.substring(0, 1).toUpperCase() + lastName2Pinyin.substring(0, 1).toUpperCase();
                format4 = lastNamePinyin.substring(0, 1).toUpperCase() + lastName2Pinyin.substring(0, 1).toUpperCase() + " " + firstName;
            } else {
                format1 = firstName + " " + lastNamePinyin;
                format2 = lastNamePinyin + " " + firstName;
                format3 = firstName + " " + lastNamePinyin.substring(0, 1).toUpperCase();
                format4 = lastNamePinyin.substring(0, 1).toUpperCase() + " " + firstName;
            }
            names.add(format1 + "," + format2 + "," + format3 + "," + format4);
        }

        //打印英文姓名的列表
        names.forEach(System.out::println);

        for (String name : names) {
            String outputFile = "query_results_first.txt";
            String csvFile =  "query_results_first.csv";
            String[] parts = name.split(",");
//            String strName = String.format("AND ((((%s[Author - First]) OR (%s[Author - First])) OR (%s[Author - First])) OR (%s[Author - First])) AND (y_5[Filter])",
//                    parts[0], parts[1], parts[2], parts[3]);
//            String strName = String.format("AND ((((%s[Author]) OR (%s[Author])) OR (%s[Author])) OR (%s[Author])) AND (y_5[Filter])",
//                    parts[0], parts[1], parts[2], parts[3]);
//            String strName = String.format(" AND ((((%s[Author - Last]) OR (%s[Author - Last])) OR (%s[Author - Last])) OR (%s[Author - Last])) AND (y_5[Filter])",
//                    parts[0], parts[1], parts[2], parts[3]);
//            String strName = String.format("AND ((((%s[Author]) OR (%s[Author])) OR (%s[Author])) OR (%s[Author])) AND (y_5[Filter]) AND (clinicaltrial[Filter])",
//                parts[0], parts[1], parts[2], parts[3]);
//            String  query= "((((((((((((((((((((((((((((((\"Diabetes Mellitus, Type 1\"[Mesh]) OR (Type 1 Diabetes[Title/Abstract])) OR (Diabetes, Type 1[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent[Title/Abstract])) OR (Diabetes Mellitus, Insulin Dependent[Title/Abstract])) OR (Insulin-Dependent Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Juvenile-Onset[Title/Abstract])) OR (Diabetes Mellitus, Juvenile Onset[Title/Abstract])) OR (Juvenile-Onset Diabetes Mellitus[Title/Abstract])) OR (IDDM[Title/Abstract])) OR (Diabetes Mellitus, Type I[Title/Abstract])) OR (Diabetes Mellitus, Sudden-Onset[Title/Abstract])) OR (Diabetes Mellitus, Sudden Onset[Title/Abstract])) OR (Sudden-Onset Diabetes Mellitus[Title/Abstract])) OR (Type 1 Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent, 1[Title/Abstract])) OR (Insulin-Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Insulin Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Juvenile-Onset Diabetes[Title/Abstract])) OR (Diabetes, Juvenile-Onset[Title/Abstract])) OR (Juvenile Onset Diabetes[Title/Abstract])) OR (Diabetes, Autoimmune[Title/Abstract])) OR (Autoimmune Diabetes[Title/Abstract])) OR (Diabetes Mellitus, Brittle[Title/Abstract])) OR (Brittle Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Ketosis-Prone[Title/Abstract])) OR (Diabetes Mellitus, Ketosis Prone[Title/Abstract])) OR (Ketosis-Prone Diabetes Mellitus[Title/Abstract]))) OR ((((\"Thyroid Diseases\"[Mesh]) OR (Diseases, Thyroid[Title/Abstract])) OR (Disease, Thyroid[Title/Abstract])) OR (Thyroid Disease[Title/Abstract])))";
//            String  query=" ((((((\"Thyroid Diseases\"[Mesh]) OR (Diseases, Thyroid[Title/Abstract])) OR (Disease, Thyroid[Title/Abstract])) OR (Thyroid Disease[Title/Abstract])) OR (((((((((((((((((((((((((((((\"Diabetes Mellitus, Type 1\"[Mesh]) OR (Type 1 Diabetes[Title/Abstract])) OR (Diabetes, Type 1[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent[Title/Abstract])) OR (Diabetes Mellitus, Insulin Dependent[Title/Abstract])) OR (Insulin-Dependent Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Juvenile-Onset[Title/Abstract])) OR (Diabetes Mellitus, Juvenile Onset[Title/Abstract])) OR (Juvenile-Onset Diabetes Mellitus[Title/Abstract])) OR (IDDM[Title/Abstract])) OR (Diabetes Mellitus, Type I[Title/Abstract])) OR (Diabetes Mellitus, Sudden-Onset[Title/Abstract])) OR (Diabetes Mellitus, Sudden Onset[Title/Abstract])) OR (Sudden-Onset Diabetes Mellitus[Title/Abstract])) OR (Type 1 Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent, 1[Title/Abstract])) OR (Insulin-Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Insulin Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Juvenile-Onset Diabetes[Title/Abstract])) OR (Diabetes, Juvenile-Onset[Title/Abstract])) OR (Juvenile Onset Diabetes[Title/Abstract])) OR (Diabetes, Autoimmune[Title/Abstract])) OR (Autoimmune Diabetes[Title/Abstract])) OR (Diabetes Mellitus, Brittle[Title/Abstract])) OR (Brittle Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Ketosis-Prone[Title/Abstract])) OR (Diabetes Mellitus, Ketosis Prone[Title/Abstract])) OR (Ketosis-Prone Diabetes Mellitus[Title/Abstract]))))) ";
//            String  searchTerm = query+strName;
            String searchTerm = String.format("(((((\"Thyroid Diseases\"[Mesh]) OR (Diseases, Thyroid[Title/Abstract])) OR (Disease, Thyroid[Title/Abstract])) OR (Thyroid Disease[Title/Abstract])) OR (((((((((((((((((((((((((((\"Diabetes Mellitus, Type 1\"[Mesh]) OR (Type 1 Diabetes[Title/Abstract])) OR (Diabetes, Type 1[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent[Title/Abstract])) OR (Diabetes Mellitus, Insulin Dependent[Title/Abstract])) OR (Insulin-Dependent Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Juvenile-Onset[Title/Abstract])) OR (Diabetes Mellitus, Juvenile Onset[Title/Abstract])) OR (Juvenile-Onset Diabetes Mellitus[Title/Abstract])) OR (IDDM[Title/Abstract])) OR (Diabetes Mellitus, Type I[Title/Abstract])) OR (Diabetes Mellitus, Sudden-Onset[Title/Abstract])) OR (Diabetes Mellitus, Sudden Onset[Title/Abstract])) OR (Sudden-Onset Diabetes Mellitus[Title/Abstract])) OR (Type 1 Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent, 1[Title/Abstract])) OR ((Insulin-Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Insulin Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Juvenile-Onset Diabetes[Title/Abstract])) OR (Diabetes, Juvenile-Onset[Title/Abstract])) OR (Juvenile Onset Diabetes[Title/Abstract])) OR (Diabetes, Autoimmune[Title/Abstract])) OR (Autoimmune Diabetes[Title/Abstract])) OR (Diabetes Mellitus, Brittle[Title/Abstract])) OR (Brittle Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Ketosis-Prone[Title/Abstract])) OR (Diabetes Mellitus, Ketosis Prone[Title/Abstract])) OR (Diabetes Mellitus, Ketosis Prone[Title/Abstract]))) AND ((((\"%s\"[Author - First]) OR (\"%s\"[Author - First])) OR (\"%s\"[Author - First])) OR (\"%s\"[Author - First]))  AND (y_5[Filter]) ",
                    parts[0], parts[1], parts[2], parts[3]);

            System.out.println("name: " + parts[0]);
            System.out.println("查询字符串: " + searchTerm);
            System.out.println();
             String apiUrl = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";
            try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                // 第一步：搜索文献获取PMID列表（支持翻页）
                int retstart = 0;
                int retmax = 100; // 每页返回的最大记录数
                boolean hasMoreResults = true;
                List<String> allPmids = new ArrayList<>();
                JsonNode idListNode = null;
                while (hasMoreResults) {
                    String searchUrl = String.format("%s?db=pubmed&term=%s&retmode=json&retstart=%d&retmax=%d",
                            apiUrl,
                            java.net.URLEncoder.encode(searchTerm, "UTF-8"),
                            retstart,
                            retmax);

                    HttpGet searchRequest = new HttpGet(searchUrl);

                    try (CloseableHttpResponse searchResponse = httpClient.execute(searchRequest)) {
                        HttpEntity searchEntity = searchResponse.getEntity();

                        if (searchEntity != null) {
                            String searchResult = EntityUtils.toString(searchEntity);
                            ObjectMapper mapper = new ObjectMapper();
                            JsonNode rootNode = mapper.readTree(searchResult);

                            // 提取PMID列表
                            idListNode = rootNode.path("esearchresult").path("idlist");
                            if (idListNode.isArray() && idListNode.size() > 0) {
                                for (JsonNode idNode : idListNode) {
                                    String pmid = idNode.asText();
                                    allPmids.add(pmid);
                                }
                                retstart += retmax; // 更新起始位置
                            } else {
                                hasMoreResults = false; // 没有更多结果，退出循环
                            }
                        }
                    }
                }

                if (!allPmids.isEmpty()) {
                    System.out.println("找到的文献PMID列表:");
                    for (String pmid : allPmids) {
                        System.out.println("PMID: " + pmid);
                    }
                    System.out.println("共找到 " + allPmids.size() + " 篇文献");
                } else {
                    System.out.println("未找到匹配的文献");
                }
                        // 第4步：将查询结果保存到本地文件

                try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile, true))) {
                    writer.println("姓名: " + name);
                    writer.println("查询字符串: " + searchTerm);
                    writer.println("PMID列表: " + allPmids.toString());
                    writer.println("合计: " + allPmids.size());
                    writer.println("----------------------------------");
                } catch (IOException e) {
                    System.err.println("保存文件时出错: " + e.getMessage());
                }
                // 第5步：如果PMID列表合计大于等于100，就使用新的字符串
                if (allPmids.size() >= 50) {

                    String newSearchTerm = String.format("(((((\"Thyroid Diseases\"[Mesh]) OR (Diseases, Thyroid[Title/Abstract])) OR (Disease, Thyroid[Title/Abstract])) OR (Thyroid Disease[Title/Abstract])) OR (((((((((((((((((((((((((((\"Diabetes Mellitus, Type 1\"[Mesh]) OR (Type 1 Diabetes[Title/Abstract])) OR (Diabetes, Type 1[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent[Title/Abstract])) OR (Diabetes Mellitus, Insulin Dependent[Title/Abstract])) OR (Insulin-Dependent Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Juvenile-Onset[Title/Abstract])) OR (Diabetes Mellitus, Juvenile Onset[Title/Abstract])) OR (Juvenile-Onset Diabetes Mellitus[Title/Abstract])) OR (IDDM[Title/Abstract])) OR (Diabetes Mellitus, Type I[Title/Abstract])) OR (Diabetes Mellitus, Sudden-Onset[Title/Abstract])) OR (Diabetes Mellitus, Sudden Onset[Title/Abstract])) OR (Sudden-Onset Diabetes Mellitus[Title/Abstract])) OR (Type 1 Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Insulin-Dependent, 1[Title/Abstract])) OR ((Insulin-Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Insulin Dependent Diabetes Mellitus 1[Title/Abstract])) OR (Juvenile-Onset Diabetes[Title/Abstract])) OR (Diabetes, Juvenile-Onset[Title/Abstract])) OR (Juvenile Onset Diabetes[Title/Abstract])) OR (Diabetes, Autoimmune[Title/Abstract])) OR (Autoimmune Diabetes[Title/Abstract])) OR (Diabetes Mellitus, Brittle[Title/Abstract])) OR (Brittle Diabetes Mellitus[Title/Abstract])) OR (Diabetes Mellitus, Ketosis-Prone[Title/Abstract])) OR (Diabetes Mellitus, Ketosis Prone[Title/Abstract])) OR (Diabetes Mellitus, Ketosis Prone[Title/Abstract]))) AND ((((\"%s\"[Author]) OR (\"%s\"[Author]))))  AND (y_5[Filter])",
                            parts[0], parts[1]);

                    System.out.println("PMID数量超过100，使用新查询字符串: " + newSearchTerm);
                    // 重新执行搜索逻辑
                    retstart = 0;
                    hasMoreResults = true;
                    allPmids.clear();

                    while (hasMoreResults) {
                        String searchUrl = String.format("%s?db=pubmed&term=%s&retmode=json&retstart=%d&retmax=%d",
                                apiUrl,
                                java.net.URLEncoder.encode(newSearchTerm, "UTF-8"),
                                retstart,
                                retmax);

                        HttpGet searchRequest = new HttpGet(searchUrl);

                        try (CloseableHttpResponse searchResponse = httpClient.execute(searchRequest)) {
                            HttpEntity searchEntity = searchResponse.getEntity();

                            if (searchEntity != null) {
                                String searchResult = EntityUtils.toString(searchEntity);
                                ObjectMapper mapper = new ObjectMapper();
                                JsonNode rootNode = mapper.readTree(searchResult);

                                // 提取PMID列表
                                idListNode = rootNode.path("esearchresult").path("idlist");
                                if (idListNode.isArray() && idListNode.size() > 0) {
                                    for (JsonNode idNode : idListNode) {
                                        String pmid = idNode.asText();
                                        allPmids.add(pmid);
                                    }
                                    retstart += retmax; // 更新起始位置
                                } else {
                                    hasMoreResults = false; // 没有更多结果，退出循环
                                }
                            }
                        }
                    }

                    if (!allPmids.isEmpty()) {
                        System.out.println("新查询找到的文献PMID列表:");
                        for (String pmid : allPmids) {
                            System.out.println("PMID: " + pmid);
                        }
                        System.out.println("共找到 " + allPmids.size() + " 篇文献");
                    } else {
                        System.out.println("新查询未找到匹配的文献");
                    }
                    // 第5-2步：将查询结果保存到本地文件
                    try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile, true))) {
                        writer.println("姓名(异常): " + name);
                        writer.println("查询字符串（优化后）: " + newSearchTerm);
                        writer.println("PMID列表: " + allPmids.toString());
                        writer.println("合计: " + allPmids.size());
                        writer.println("----------------------------------");
                    } catch (IOException e) {
                        System.err.println("保存文件时出错: " + e.getMessage());
                    }
                }
                //第6步：将查询结果保存到本地CSV文件中，用于后续分析，csv文件中有两列：姓名和合计。
                try (PrintWriter writer = new PrintWriter(new FileWriter(csvFile, true))) {
                    writer.println(name + "," + allPmids.size());
                } catch (IOException e) {
                    System.err.println("保存文件时出错: " + e.getMessage());
                }
            }catch(IOException e) {
                System.err.println("网络请求时出错: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}

